package personal.wang.pekko;

import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.*;
import org.apache.pekko.pattern.Patterns;

import java.time.Duration;
import java.util.concurrent.CompletionStage;

public class Client extends AbstractActor {
    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("flink", ConfigFactory.load("test_client"));
        ActorRef client = actorSystem.actorOf(Props.create(Client.class), "client");
        client.tell("hello server" , ActorRef.noSender());
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(this::handler).build();
    }

    private void handler(Object o) {
        // pekko://flink@127.0.0.1:17354/user/server
        ActorSelection actorSelection = context().actorSelection("pekko://flink@127.0.0.1:17354/user/server");
//        actorSelection.tell(o , getSelf());
        MessageRecord msg = new MessageRecord("hello server");
        CompletionStage<Object> ask = Patterns.ask(actorSelection, msg, Duration.ofSeconds(3));
        ask.handle((r , throwable) -> {
            System.out.println(r);
            System.out.println(throwable);
            return null ;
        });
    }
}
